import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController, Events } from 'ionic-angular';
import { AppGlobal } from '../../providers/appGlobal'
import { UserProvider } from '../../providers/service/user'
import { AboutPage } from './about/about';
import { ChangePwdPage } from './change-pwd/change-pwd';
import { SettingPage } from './setting/setting';
/**
 * Generated class for the UserPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-user',
  templateUrl: 'user.html',
})
export class UserPage {

  constructor(public navCtrl: NavController,
    public userProvider: UserProvider,
              public event: Events,
      public alertCtrl: AlertController,
     public navParams: NavParams) {
  }

  userObj;

  ionViewDidLoad() {
    console.log('ionViewDidLoad UserPage');
    if(AppGlobal.getInstance().getUserInfo()){
      this.userObj = AppGlobal.getInstance().getUserInfo();
      console.log(this.userObj)
    }else{
      this.userProvider.getUserInfoByToken().then(res=>{
        this.userObj = AppGlobal.getInstance().getUserInfo();
        console.log(this.userObj)
      },err=>{

      })
    }
    
  }

  pToChangePwd(){
    this.navCtrl.push(ChangePwdPage);
  }
  
  pToAbout(){
    this.navCtrl.push(AboutPage);
  }
  
  pToSetting(){
    this.navCtrl.push(SettingPage);
  }
  logout(){
    let alert = this.alertCtrl.create({
      title: "退出",
      subTitle: '确认退出此账号?',
      buttons: [
        "取消",
        {
          text: "确认",
          handler: data => {
            this.event.publish('LOGOUT');
          }
        }
      ]
    });
    alert.present();
  }

}
